package com.auxtools;

import com.appfront.TestproSetup;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.PointValue;

/**
 * Created by westl on 2018/9/15 0015.
 */

public class LINE {

    static final float k_f = (float) (0.22472);
    static final int trendNums = 5;
    public static float trendBase = 0;
    public List<PointValue> Xval = new ArrayList<PointValue>();
    public Line line = new Line(Xval);
    public float max_Y;
    public float min_Y;

    public static enum TREND{      //趋势
        NONE,               //无趋势
        JUSTUP,             // 仅仅上升
        JUSTDOWN,           // 仅仅下降
    }
    private int maxLineLength = 400;

    public TREND trendJudge(){
        int listLen = Xval.size();
        if(listLen <= trendNums)
            return TREND.NONE;

        int i=0;
        for(i=0; i<trendNums; i++){         //下降趋势判断：回到原点
            if(Xval.get(listLen-trendNums+i-1).getY() <= Xval.get(listLen-trendNums+i).getY()){
                break;
            }
        }
        if(i==trendNums){
            return TREND.JUSTDOWN;
        }
        for(i=0; i<trendNums; i++){         //上升趋势判断：离开原点
            if(Xval.get(listLen-trendNums+i-1).getY() >= Xval.get(listLen-trendNums+i).getY()){
                break;
            }
        }
        if(i==trendNums){
            return TREND.JUSTUP;
        }
        return TREND.NONE;
    }

    public LINE(List<Line>  chartLines, int color) {
        line.setColor(color);
        line.setStrokeWidth(2);
        line.setCubic(true);        //是否平滑
        line.setHasLabelsOnlyForSelected(true);
        line.setHasLines(true);     //是否用线显示
        line.setHasPoints(false);   //是否显示圆点
        chartLines.add(line);
    }

    public LINE(List<Line>  chartLines, int color, boolean isCube, boolean isPot) {
        line.setColor(color);
        line.setStrokeWidth(2);
        line.setCubic(isCube);        //是否平滑
        line.setHasLabelsOnlyForSelected(true);
        line.setHasLines(true);     //是否用线显示
        line.setHasPoints(isPot);   //是否显示圆点
        chartLines.add(line);
    }

    public void removeBefore(int lens){
        int listLen = Xval.size();
        if(listLen < lens)
            return;

        for(int i=0; i<listLen-lens; i++){
            Xval.remove(0);
        }
    }

    public void setMaxLineLen(int length){
        maxLineLength = length;
    }

    public boolean isNeedClear(){
        if(Xval.size() > maxLineLength)
            return  true;
        else
            return  false;
    }

    public void add(float x, float y) {
        Xval.add(new PointValue(x, y));
        if(Xval.size() > maxLineLength){
            Xval.remove(0);
        }
        if(y > max_Y)
            max_Y = y;
        else if(y < min_Y)
            min_Y = y;
    }

    public void clear() {
        Xval.clear();
        max_Y = 0;
        min_Y = 0;
    }

    public float getCublicValue(int nums, boolean noMax){
        int listLen = Xval.size();
        if(listLen < nums){
            return Xval.get(listLen-1).getY();
        }else{
            float sum = Xval.get(listLen-nums).getY();
            float max = sum;
            float min = sum;
            float vtemp;
            for(int i=1; i<nums; i++){
                vtemp = Xval.get(listLen-nums+i).getY();
                sum += vtemp;
                if(max < vtemp)
                    max = vtemp;
                else if(min > vtemp)
                    min = vtemp;
            }
            if(noMax){
                return  (sum-max-min)/(nums-2);
            }else{
                return  sum/nums;
            }
        }
    }

    public float getLastStepTimes(){
        int length = Xval.size();
        return  Xval.get(length-1).getX() - Xval.get(length-2).getX();
    }

    public boolean isBanlance(int length){
        if(length > Xval.size())
            return  false;
        int totalLen = Xval.size();
        float average = 0;
        for(int i=totalLen-length; i<totalLen; i++){
            average += Xval.get(i).getY();
        }
        average = average/length;
        for(int i=totalLen-length; i<totalLen; i++){
            if(Math.abs(Xval.get(i).getY() - average) > average*(1-TestproSetup.forceScaleMax))
                return false;
        }
        return true;
    }

    /* 获取最大值维持时间，单位：s */
    public float getDurTimes(float maxForce){
        float times = 0;
        for(int i=1; i<Xval.size(); i++){
            if(Xval.get(i-1).getY() > maxForce*TestproSetup.forceScaleMax &&
                    Xval.get(i).getY() > maxForce*TestproSetup.forceScaleMax){
                times += (Xval.get(i).getX()-Xval.get(i-1).getX());
            }
        }
        return times;
    }

    private float getShift(float fa, float fb){
        double shift_a, shift_b;
        int line_pos = TestproSetup.getCurLines();
        double[] line_switch;
        switch (line_pos){
            case 1: line_switch = TestproSetup.line_peach;
                break;
            case 2: line_switch = TestproSetup.line_orange;
                break;
            case 3: line_switch = TestproSetup.line_green;
                break;
            case 4: line_switch = TestproSetup.line_blue;
                break;
            case 5: line_switch = TestproSetup.line_plum;
                break;
            case 6: line_switch = TestproSetup.line_gray;
                break;
                default:
                    return 0;
        }
        int index = 0;
        for(index=0; index<12; index++){
            /* 这里有个比例关系 */
            if(fa*k_f > line_switch[index]){
                continue;
            }else{
                break;
            }
        }
        if(index == 11){
            shift_a = TestproSetup.line_scale[index]*TestproSetup.line_raw_len;
        }else{
            shift_a = (TestproSetup.line_scale[index] +
                    (fa*k_f-line_switch[index])/
                            (line_switch[index+1] - line_switch[index])*
                            (TestproSetup.line_scale[index+1]-TestproSetup.line_scale[index]))*TestproSetup.line_raw_len
                    ;
        }
        for(index=0; index<12; index++){
            /* 这里有个比例关系 */
            if(fb*k_f > line_switch[index]){
                continue;
            }else{
                break;
            }
        }
        if(index == 11){
            shift_b = TestproSetup.line_scale[index]*TestproSetup.line_raw_len;
        }else{
            shift_b = (TestproSetup.line_scale[index] +
                    (fb*k_f-line_switch[index])/
                            (line_switch[index+1] - line_switch[index])*
                            (TestproSetup.line_scale[index+1]-TestproSetup.line_scale[index]))*TestproSetup.line_raw_len
                    ;
        }
        return (float) (shift_b-shift_a);
    }

    private float lastPower = 0;
    public float getNowPower(){

        int listLen = Xval.size();
        if(listLen < 2)
            return 0;
        float fb, fa, tb, ta;
        fb = Xval.get(listLen-1).getY();
        tb = Xval.get(listLen-1).getX();
        fa = Xval.get(listLen-2).getY();
        ta = Xval.get(listLen-2).getX();
        if(fb >= fa){
            lastPower = (float)Math.abs(0.1*fb*getShift(fa, fb)/(tb-ta));
        }
        return  lastPower; //t: ms
    }

    float getMax_Y(){
        return max_Y;
    }
    float getMin_Y(){
        return  min_Y;
    }
}
